<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of the Vulpes backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with the Vulpes back-end, such as the Vulpes front-end by LinFox,
	is subject to it's correspending licene. */

class plan
{
	public $id;
	public $name;
	public $description;
	public $price;
	public $download;
	public $upload;
	public $interface;
	public $iface;
	public $schedules;
	public $exists;
	private $db;
	
	function __construct($id = false)
	{
		global $db, $lang;
		$this->db =& $db;
		$this->schedules = array();
		
		if ($id) {
			$this->id = $id;
			$plan_sql = '
				SELECT * FROM `plans`
				WHERE `id` = "'.$this->id.'"';
			if (!$plan_result = $this->db->query($plan_sql)) return;
			$plan = $plan_result->fetch_object();
			foreach ($plan as $key => $value) {
				$this->$key = $value;
			}
			$this->iface = $this->interface;
			$this->exists = true;

			$schedules_sql = '
				SELECT * FROM `schedules`
				WHERE `plan_id` = "'.$this->id.'"';
			if (!$schedules_result = $this->db->query($schedules_sql)) return;
			for ($i = 0; $this->schedules[$i] = $schedules_result->fetch_object(); $i++) {
				$this->schedules[$i]->day_name = $lang[$this->schedules[$i]->day];
			}
		}
	}

	function insert()
	{
		if ($this->exists) return false;
		$sql_save = '
			INSERT INTO `plans`
			SET	`name` = "' .$this->name. '",
				`description` = "' .$this->description. '",
				`price` = "' .$this->price. '",
				`download` = "' .$this->download. '",
				`upload` = "' .$this->upload. '",
				`interface` = "' .$this->interface. '",
				`auth` = "' .$this->auth. '"';
		if (!$this->db->query($sql_save)) return false;

		return true;
	}
	
	function update()
	{
		if (!$this->exists) return false;
		$sql_save = '
			UPDATE `plans`
			SET	`name` = "' .$this->name. '",
				`description` = "' .$this->description. '",
				`price` = "' .$this->price. '",
				`download` = "' .$this->download. '",
				`upload` = "' .$this->upload. '",
				`interface` = "' .$this->interface. '",
				`auth` = "' .$this->auth. '"
			WHERE `id` = "'.$this->id.'"';
		if (!$this->db->query($sql_save)) return false;

		return true;
	}
	
	function delete()
	{
		if (!$this->exists) return false;
		$sql_delete = '
			DELETE FROM `plans`
			WHERE `id` = "'.$this->id.'"';
		if (!$this->db->query($sql_delete)) return false;

		return true;
	}
	
	/**
	 * Inserts a schedule for this plan.
	 */
	function insert_schedule($day,$start,$end,$upload,$download)
	{
		$insert_schedule_sql = '
			INSERT INTO `schedules`
			SET `plan_id` = "'.$this->id.'",
				`day` = "'.$day.'",
				`start` = "'.$start.'",
				`end` = "'.$end.'",
				`upload` = "'.$upload.'",
				`download` = "'.$download.'"';
		if (!$this->db->query($insert_schedule_sql)) return false;

		return true;
	}
	
	function remove_schedule($id)
	{
		$remove_schedule_sql = '
			DELETE FROM `schedules`
			WHERE `id` = "'.$id.'"';
		if (!$this->db->query($remove_schedule_sql)) return false;

		return true;
	}
}

?>